<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * 应用程序相关的操作
 *
 * @author YL-Z0016
 */
class AppModel {
    /**
     *获取全部的应用 
     */
    public function getAllApp(){
        $db = getDB();
        return $db->get_results('SELECT Fappid AS appid,Fappname AS appname FROM t_app');
    }
    /**
     *获取用户参与的应用
     * @param type $uid 
     */
    public function getApps($uid){
        $db = getDB();
        $list = $db->get_results(sprintf('SELECT Fappid AS appid,Fappname AS appname FROM t_user_app WHERE Fuid=%d',$uid));
        return $list;
    }
    /**
     *添加一个应用
     * @param type $name 
     */
    public function addApp($name){
        $db = getDB();
        $set = array();
        $set['Fappname'] = $name;
        return $db->insertTable('t_app',$set);
    }
    /**
     *修改一个应用
     * @param type $appid
     * @param type $name 
     */
    public function updateApp($appid,$name){
        $db = getDB();
        $set = array();
        $set['Fappname'] = $name;
        return $db->updateTable('t_app',$set,'Fappid='.$appid);
    }
    /**
     *用户添加一个应用
     * @param type $uid
     * @param type $appid 
     */
    public function userAddApp($uid,$appid){
        $db = getDB();
        if(!$db->get_row(sprintf('SELECT * FROM t_user_app WHERE Fuid=%d AND Fappid=%d',$uid,$appid))){
            $set= array();
            $set['Fuid'] = $uid;
            $set['Fappid'] = $appid;
            return $db->insertTable('t_user_app',$set);
        }
        return false;
    }
    /**
     *用户删除一个应用
     * @param type $uid
     * @param type $appid 
     */
    public function userDelApp($uid,$appid){
        $db = getDB();
        return $db->delRow('t_user_app',sprintf('Fuid=%d AND Fappid=%d',$uid,$appid));
    }
    
}

?>
